﻿namespace WMS6.Web.TransportManagement
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.TransportMgtServiceRef;
    using WMS6.Web.UserControls;

    public class PODList : BasePage
    {
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F8052;
        protected GridView gvPod;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected Label lblMsg;
        protected Label lblUom;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2PodList(PodQueryEntity podQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(podQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = TransportMgtServiceClientProxy.GetInstance().GetPodInfoByQueryEntity(podQuery);
            base.BindData2GridView(this.gvPod, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPod, "POD_ID");
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    this.lblMsg.Text = TransportMgtServiceClientProxy.GetInstance().DeletePodInfo(selectedID) + base.GetGlobalMsgResource("C006");
                    PodQueryEntity podQuery = this.GetPodQueryEntity();
                    this.BindData2PodList(podQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPod, "POD_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                base.Response.Redirect(string.Format("POD.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            PodQueryEntity podQuery = this.GetPodQueryEntity();
            podQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = podQuery.CurrentPage;
            podQuery.CurrentPage = "0";
            this.gvPod.Columns[1].Visible = false;
            this.gvPod.AllowSorting = false;
            this.BindData2PodList(podQuery);
            this.hdnActionID.Value = "";
            this.gvPod.ShowFooter = false;
            podQuery.IsGetAll = false;
            podQuery.CurrentPage = currentPageIndex;
            this.SetPodQueryEntity(podQuery);
            base.Export2Excel("Pod.xls", this.gvPod);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            PodQueryEntity podQuery = this.GetPodQueryEntity();
            podQuery.CurrentPage = "0";
            this.BindData2PodList(podQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            PodQueryEntity podQuery = this.GetPodQueryEntity();
            podQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2PodList(podQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            PodQueryEntity podQuery = this.GetPodQueryEntity();
            podQuery.DeliveryID = "";
            podQuery.CarrierID = "";
            podQuery.CreatedBy = "";
            podQuery.BolID = "";
            podQuery.Status = "";
            podQuery.SignedBy = "";
            podQuery.SignedDate = "";
            this.BindData2PodList(podQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            PodQueryEntity podQuery = this.GetPodQueryEntity();
            if (int.Parse(podQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                podQuery.CurrentPage = (int.Parse(podQuery.CurrentPage) + 1).ToString();
                this.BindData2PodList(podQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            PodQueryEntity podQuery = this.GetPodQueryEntity();
            if (int.Parse(podQuery.CurrentPage) > 0)
            {
                podQuery.CurrentPage = (int.Parse(podQuery.CurrentPage) - 1).ToString();
                this.BindData2PodList(podQuery);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("PODQuery.aspx");
        }

        private PodQueryEntity GetPodQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as PodQueryEntity);
            }
            return (base.PageQueryEntitySession as PodQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                PodQueryEntity podQuery = this.GetPodQueryEntity();
                podQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2PodList(podQuery);
            }
        }

        protected void gvPod_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F8052", this.gvPod);
        }

        protected void gvPod_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvPod_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                PodQueryEntity podQuery = this.GetPodQueryEntity();
                podQuery.SortField = e.SortExpression;
                if (podQuery.SortDirection == "ASC")
                {
                    podQuery.SortDirection = "DESC";
                }
                else if (podQuery.SortDirection == "DESC")
                {
                    podQuery.SortDirection = "ASC";
                }
                this.BindData2PodList(podQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            PodQueryEntity podQuery = new PodQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                podQuery.DeliveryID = this.PreviousPage.DeliveryOrderID;
                podQuery.CarrierID = this.PreviousPage.CarrierID;
                podQuery.CreatedBy = this.PreviousPage.CreateBy;
                podQuery.BolID = this.PreviousPage.BolDetailID;
                podQuery.Status = this.PreviousPage.Status;
                podQuery.SignedBy = this.PreviousPage.SignBy;
                podQuery.SignedDate = this.PreviousPage.SignDate;
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            podQuery.IsGetAll = false;
            podQuery.CurrentPage = "0";
            podQuery.PageSize = this.gvPod.PageSize.ToString();
            podQuery.SortField = "POD.ROWID";
            podQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvPod.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvPod.Columns[1].Visible = false;
                this.gvPod.Columns[2].Visible = true;
                this.btnEdit.Visible = false;
                this.btnDelete.Visible = false;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
            }
            else if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetPodQueryEntity(podQuery);
            this.BindData2PodList(podQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetPodQueryEntity(PodQueryEntity podQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = podQuery;
            }
            else
            {
                base.PageQueryEntitySession = podQuery;
            }
        }

        public PODQuery PreviousPage
        {
            get
            {
                return (PODQuery) base.PreviousPage;
            }
        }
    }
}

